//
//  HomeCellXib.m
//  MyBlog
//
//  Created by NCS on 16/12/7.
//  Copyright © 2016年 YSL. All rights reserved.
//

#import "HomeCell.h"
#import "YSLScanImage.h"

float COLLECTION_CELL_GAP = 10;

@implementation HomeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.cellImages.delegate = self;
    self.cellImages.dataSource = self;
    
    self.cellImages.backgroundColor = [UIColor clearColor];
    
    // Register nib file for the cell
    UINib *nib = [UINib nibWithNibName:@"CollectionCell" bundle: [NSBundle mainBundle]];
    [self.cellImages registerNib:nib forCellWithReuseIdentifier:CollectionViewCellIdentifier];
    
    imageSize = DScreenWidth / 3 - 10;
}

- (void)updateConstraints {
    [super updateConstraints];
    
    // Reset the height of the collection view
    long imagesCount = [self.homeCellModel.images count];
    
    if (imagesCount == 0) {
        self.collectionHeightConstraint.constant = 0;
    } else if (imagesCount >= 1 && imagesCount <= 3){
        self.collectionHeightConstraint.constant = imageSize    ;
    } else if (imagesCount > 3 && imagesCount <= 6){
        self.collectionHeightConstraint.constant = imageSize * 2 + COLLECTION_CELL_GAP;
    } else if (imagesCount > 6 && imagesCount <= 9){
        self.collectionHeightConstraint.constant = imageSize  * 3 + COLLECTION_CELL_GAP * 3;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setCellData:(HomeCellModel *)model {
    self.homeCellModel = [[HomeCellModel alloc] initWithModel:model];
}


#pragma mark -- UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return [self.homeCellModel.images count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    // Set up the reuse identifier
    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CollectionViewCellIdentifier forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor clearColor];
    
    [[SDWebImageDownloader sharedDownloader] setValue:@"iPhone" forHTTPHeaderField:@"User-Agent"];
    
//    [cell setCellImageWithName:self.homeCellModel.images[indexPath.row]];
//    NSURL *url = [NSURL URLWithString:@"http://img3.imgtn.bdimg.com/it/u=3779691387,1388917972&fm=23&gp=0.jpg"];
    NSURL *url = [NSURL URLWithString:self.homeCellModel.images[indexPath.row]];
    [cell setCellImageWithURL:url];
        
    return cell;

}

//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    NSLog(@"------------touchesBegan---------- %@ : ", self.dataIndex);
//}

#pragma mark -- Collection delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Tableviewcell %ld -- Image %ld has been selected!", self.dataIndex.row, indexPath.row);
    
    CollectionCell *cell = (CollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    [YSLScanImage scanBigImageWithImageView:cell.collectionImage];
}

#pragma mark -- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    // Change the size of each item in the collectionView
    return CGSizeMake(imageSize, imageSize);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return COLLECTION_GAP;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 2;
}

@end
